/*
 * Copyright (C) 2008 aki@akjava.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.akjava.android.openglsamples.glut;

import java.nio.FloatBuffer;

import javax.microedition.khronos.opengles.GL10;

import android.app.Activity;

import com.akjava.android.openglsamples.GLUT;
import com.akjava.android.openglsamples.OpenGLUtils;

public class CubeRenderer extends GLUTSimpleRenderer{

    public CubeRenderer(Activity context) {
       super(context);
    }


    public void initialize() {
        mFVertexBuffer = OpenGLUtils.allocateFloatBuffer(1000);
        mColorBuffer = OpenGLUtils.allocateFloatBuffer(1000);
        mIndexBuffer = OpenGLUtils.allocateShortBuffer(1000);
        
        
        OpenGLUtils.addVertex3f(mFVertexBuffer, -0.5f	,0.5f, 0);
        OpenGLUtils.addVertex3f(mFVertexBuffer, 0.5f	,-0.5f, 0);
        
        OpenGLUtils.addVertex3f(mFVertexBuffer, 0.5f	,0.5f, 0);
        OpenGLUtils.addVertex3f(mFVertexBuffer, -0.5f	,-0.5f, 0);

        
       float cube_diffuse[] = { 0.0f, 0.7f, 0.7f, 1.0f };
   	   
   	   cube_diffuseBuffer=OpenGLUtils.toFloatBufferPositionZero(cube_diffuse);
        
        mFVertexBuffer.position(0);
        mColorBuffer.position(0);
        mIndexBuffer.position(0);
        

    }

    
    private FloatBuffer cube_diffuseBuffer;

    public void displayObjects(GL10 gl) {

    	gl.glRotatef (45.0f, 0.0f, 0.0f, 1.0f);
  	    gl.glRotatef (45.0f, 1.0f, 0.0f, 0.0f);
  	  
    	if(wireFrame){
    	GLUT.glutWireCube(gl,2.5f);	
    	}else{
    	GLUT.glutSolidCube(gl, 2.5f);
    	}
    }



    
    public void display(GL10 gl) {
    	 //someway need 
    	
   	    gl.glMatrixMode(GL10.GL_MODELVIEW);
        gl.glLoadIdentity();
        //GLU.gluLookAt(gl, 0, 0, 7f, 0f, 0f, 0f, 0f, 1.0f, 0.0f);
        gl.glEnable(GL10.GL_CULL_FACE);	//especiall box need it.
       
        
        gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
        //move
        gl.glRotatef(rightLeftValue, 0, 1f, 0);
  	    gl.glRotatef(topDownValue, 1f, 0, 0);
        displayObjects(gl);
    }

    

	@Override
	public void init(GL10 gl) {
		float mat_ambient[] = { 1.0f, 1.0f, 1.0f, 1.0f};
		float mat_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
		float light_position[] = { 0.0f, 0.0f, 10.0f, 1.0f };
		//float lm_ambient[] = { 0.2f, 0.2f, 0.2f, 1.0f };
		float lm_ambient[] = { 0.2f, 0.2f, 0.2f, 1.0f };
		float lm_diffuse[] = new float[] { 1f, 1f, 1f, 1.0f };
		   gl.glMaterialfv(GL10.GL_FRONT_AND_BACK, GL10.GL_AMBIENT, mat_ambient,0);	//GL_FRONT is wrong?
		   gl.glMaterialfv(GL10.GL_FRONT_AND_BACK, GL10.GL_SPECULAR,mat_specular,0);
		   gl.glMaterialfv(GL10.GL_FRONT_AND_BACK, GL10.GL_DIFFUSE, cube_diffuseBuffer);
		   gl.glMaterialf(GL10.GL_FRONT_AND_BACK, GL10.GL_SHININESS, 50.0f);
		   gl.glLightfv(GL10.GL_LIGHT0, GL10.GL_POSITION, OpenGLUtils.toFloatBufferPositionZero(light_position));
		   //gl.glLightfv(GL10.GL_LIGHT0, GL10.GL_AMBIENT, OpenGLUtils.toFloatBuffer(lm_ambient));
		   gl.glLightfv(GL10.GL_LIGHT0, GL10.GL_DIFFUSE, OpenGLUtils.toFloatBufferPositionZero(lm_diffuse));
		   gl.glLightModelfv(GL10.GL_LIGHT_MODEL_AMBIENT, OpenGLUtils.toFloatBufferPositionZero(lm_ambient));

		   gl.glEnable(GL10.GL_LIGHTING);
		   gl.glEnable(GL10.GL_LIGHT0);
		   gl.glEnable(GL10.GL_DEPTH_TEST);
		   gl.glShadeModel (GL10.GL_FLAT);

		   gl.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
 	  
		   gl.glDepthFunc(GL10.GL_LEQUAL);
		   gl.glClearDepthf(1.0f);
	}
	
	int[] vPort = new int[4];
    public void sizeChanged(GL10 gl, int w, int h) {
    	
    		vPort[2]=w;
    		vPort[3]=h;
    	
    	   gl.glViewport(0, 0, w, h);
    	   gl.glMatrixMode(GL10.GL_PROJECTION);
    	   gl.glLoadIdentity();
    	   
    	   if (w <= h) 
    	      gl.glOrthof(-2.25f, 2.25f, 
    	         -2.25f*(float)h/(float)w, 2.25f*(float)h/(float)w,-10,10);
    	   else 
    		   gl.glOrthof (-2.25f*(float)w/(float)h, 
    	    		  2.25f*(float)w/(float)h, -2.25f, 2.25f,-10,10);
    	   gl.glMatrixMode(GL10.GL_MODELVIEW);
    	   gl.glLoadIdentity();
    	   
        
    }
    
}
